﻿using System;
using System.Windows;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows.Controls;
using Pages = MEFLauncher.MyPages;

namespace MEFLauncher
{

    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary> 

    class AddInView
    {
        [ImportMany(AllowRecomposition =true)]
        public IMEFLauncherExtension[] MEFLauncherExtensions { get; set; }
        public AddInView()
        {
            #region 检索特定目录下的插件
            var catalog = new DirectoryCatalog
                (Environment.CurrentDirectory + "\\AddIns");
            #endregion

            #region 初始化插件
            var container = new CompositionContainer(catalog);
            #endregion

            #region 报错提醒
            try
            {
                container.ComposeParts(this);
            }
            catch (ChangeRejectedException ex)
            {
                MessageBox.Show(ex.Message,"Errow!!!");
            }
            #endregion
        }

        #region 获取UI

        #region GetHomePageUI
        public FrameworkElement GetHomepageUI(int HomepageUI)
        {
            return MEFLauncherExtensions[HomepageUI].GetHomepageUI();
        }
        #endregion

        #region GetIcon
        public FrameworkElement GetIcon(int Icon)
        {
            return MEFLauncherExtensions[Icon].GetIcon();
        }

        #endregion

        #endregion
    }

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            //UIBorder.Child = new MyPages.ControlTest();

            #region 定义ListBox要显示什么
            var adnview = new AddInView();
            MyAddIns.ItemsSource = adnview.MEFLauncherExtensions;
            #endregion

            #region Version & so on...

            string LauncherVersion = "MEFLauncher 0.0.0.3";
            MainWindowWindow.Title = LauncherVersion;

            string Copyright = "(C)SoftPinkSP 2021";
            CopyrightTB.Text = Copyright;
            #endregion

            #region 头部(Headers)实现

            #region buttons

            /*#region settingsButoon
            Button settingsButoon = new Button()
            {
                Content = new MyHeaders.Settings(),
            };
            settingsButoon.Click += SettingsButoon_Click; ;
            #endregion

            #region CustomControls
            Button CustomControlsButton = new Button
            {
                Content = new CustomControl(),
            };
            CustomControlsButton.Click += CustomControlsButton_Click;
            #endregion

            #endregion

            #region HeaderEnvents
            LuncherHeader.Children.Add(settingsButoon);
            LuncherHeader.Children.Add(CustomControlsButton);
            #endregion*/

            #endregion

            #endregion

            #region 插件图标的实现
           //var AddInIconTest = Template.FindName("IconBorder",this.MyAddIns) as Border;
            //AddInIconTest.Child = adnview.GetIcon(); 
            #endregion
        }

        /*#region HerderBottonEnvents
        private void SettingsButoon_Click(object sender, RoutedEventArgs e)
        {
            UIBorder.Child = new SettingTabPage();
        }

        private void CustomControlsButton_Click(object sender, RoutedEventArgs e)
        {
            UIBorder.Child = new Null();
        }
        #endregion*/


        private void MyAddIns_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {


            ListBox lists = sender as ListBox;
            int index = lists.SelectedIndex;
            var MEFLauncherExtension = lists.ItemsSource as IMEFLauncherExtension[];
            //（双击后）可添加一个选项卡，标题为插件名称，内容为插件页面。
            TabItem newTabPage = new TabItem
            {
                Header = MEFLauncherExtension[index].Title,
                Content = MEFLauncherExtension[index].GetHomepageUI()
            };

            UI.Items.Add(newTabPage);
        }

        #region 有用的代码，别删

        private void Info_Click(object sender, RoutedEventArgs e)
        {
            Infomaition i1 = new Infomaition();

            TabItem InfoMation = new TabItem
            {
                Header = "More..",
                Content = i1
            };

            //this.UI.Items.Add(InfoMation);

        }

        #endregion

        private void Launcher_Click(object sender, RoutedEventArgs e)
        {
            //Todo 启动游戏
        }

    }
}
